package com.ouc.yyi.springcloud.gateway.enfilter;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.List;

/**
 * @description:
 * @author: yyi
 * @date: 2022/9/2 9:59
 * @version: 1.0
 **/

@Component
@Slf4j
public class UserCustomerFilter implements GatewayFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        URI uri = exchange.getRequest().getURI();
        log.info("自定义过滤器中的URI为:" + uri);
        exchange.getRequest().getHeaders();
        exchange.getResponse().getHeaders().add("123", "222");
        List<String> operKeys = exchange.getRequest().getHeaders().get("operKey");
        String first = CollUtil.getFirst(operKeys);
        if (!"766876".equals(first)) {
            throw new RuntimeException("当前的operKey不匹配");
        }
        return chain.filter(exchange);

    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}
